const button = new DOMParser().parseFromString(
  "<button style='position: fixed;top:0px;right:0;z-index:9000;line-height: 40px;background: #879bff;border: 1px solid #5a88f8;'>Click to open side panel</button>",
  "text/html"
).body.firstElementChild;
button.addEventListener("click", function () {
  chrome.runtime.sendMessage({ type: "open_side_panel" });
  setTimeout(() => {
    chrome.runtime.sendMessage({ url: document.location.href });
  }, 300);
});
document.body.append(button);

document.addEventListener("mouseup", function (event) {
  var selectedText = window.getSelection().toString();
  console.log(selectedText, "-------------");
  // 向background script发送消息
  if (selectedText) {
    chrome.runtime.sendMessage({ text: selectedText });
  }
});
chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) {
  appendDiv(request.pop)
  console.log(sender);
  sendResponse({ fromcontent: "This message is from content.js" });
});
function appendDiv(txt) {
  const div = new DOMParser().parseFromString(
    "<div style='position: fixed;top:0px;left:0;z-index:9000;line-height: 40px;background: #879bff;border: 1px solid #5a88f8;'>" +
      txt +
      "</div>",
    "text/html"
  ).body.firstElementChild;
  document.body.append(div);
}
